﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Binding
    {
        //private string m_name;
        //private string m_discription;// as at sreen
        private bool isActive;
        private SmartHouseEvent targetEvent;
        private Operation targetOperation;

        public Operation TargetOperation
        {
            get { return targetOperation; }
            set { targetOperation = value; }
        }

        public bool IsActive
        {
            get { return isActive; }
            set 
            {
                
                if (isActive==false&&value==true)
                {
                    Activate();
                    
                }
                if (isActive == true && value == false)
                {
                    Deactivate();
                }
                isActive = value;
            }
        }

        public SmartHouseEvent TargetEvent
        {
            get { return targetEvent; }
            set { targetEvent = value; }
        }

        public Binding(SmartHouseEvent tEvent, Operation tOperation)
        {
            targetEvent = tEvent;
            targetOperation = tOperation;
            this.IsActive = true;
        }

        private void Activate()
        {
            targetEvent.Triggered += new EventHandler(m_targetEvent_Triggered);
        }

        private void Deactivate()
        {
            targetEvent.Triggered -= new EventHandler(m_targetEvent_Triggered);
        }

        void m_targetEvent_Triggered(object sender, EventArgs e)
        {
            targetOperation.Perform();
        }

    }
}
